package apig.servlet.access;

import java.util.*;

import apig.client.serviface.*;

public class ServiceAccessManager {

	private static ServiceAccessManager manager;
	
	private Map<String, PermissionPolicy> policyMap = new HashMap<String, PermissionPolicy>();
	
	public static ServiceAccessManager getManager() {
		if (manager == null) {
			manager = new ServiceAccessManager();			
		}
		return manager;
	}
	
	private ServiceAccessManager() {
		// fill manager with policies
					;
	}
	
	public void registerService(String name, PermissionPolicy pp) {
		policyMap.put(name, pp);
	}
	
	public boolean checkAccess(UserData ud, String resourceName, ResourceDescription rd) {
		PermissionPolicy<ResourceDescription> pp = policyMap.get(resourceName);
		// what is not permitted directly, is denyed
		if (pp == null) {
			return false;
		} else {
			return pp.isPermissionGranted(ud, rd);
		}
	}
}
